/*
 ============================================================================
 Name        : tcu_update.c
 Author      : liutianpeng
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>

#define DOWNLOAD_PATH  "/usr/local/sanway/download/"
#define UPDATE_DEBUG
#define SCAN_TIME 5

int parse_file_name_md5(char* md5, int md5_len, char* filename)
{

	char* pos = NULL;
	char* begin = "install_";
	char* end = ".tar.gz";
	char* md5_begin = NULL;
	char* md5_end = NULL;
	int len = 0;

	if(md5==NULL || filename==NULL)
		return -1;

#ifdef UPDATE_DEBUG
	printf("##Tcu Update## enter %s\n",__func__);
#endif

	if(strncmp(filename,begin,strlen(begin)) == 0){//先查找"install_"开头的安装包
		pos = strchr(filename,'.');//再查找“.tar.gz”格式的压缩包
		if(pos != NULL){
			if(strncmp(pos,end,strlen(end)) == 0){
				md5_begin = strchr(filename,'_');
				md5_end = strchr(filename,'.');
				md5_begin += 1;
				len = md5_end - md5_begin ;
				strncpy(md5,md5_begin, len>(md5_len-1)?(md5_len-1):len);

				return 0;
			}
		}
	}

	return -1;
}

int get_file_md5(char* md5, int md5_len, char*filepath)
{
	char cmd[128] = { '\0' };
	char md5path[64] = { '\0' };
	char *pos = NULL;
	FILE* fp = NULL;
	char *line = NULL;
	size_t len = 0;

	if(md5==NULL || filepath==NULL)
		return -1;

#ifdef UPDATE_DEBUG
	printf("##Tcu Update## enter %s\n",__func__);
#endif

	strncpy(md5path, "/var/log/update_md5sum.log", sizeof(md5path));
	sprintf(cmd, "md5sum %s > %s", filepath, md5path);
	system(cmd);

	fp = fopen(md5path, "r");
	if (fp == NULL) {
		return -1;
	}

	if (getline(&line, &len, fp) == -1) {
		return -1;
	}
	pos = strchr(line, ' ');
	strncpy(md5, line, ((pos - line) > (md5_len-1) ? (md5_len-1) : (pos - line)));

	if (line)
		free(line);

	return 0;
}

int main(void) {
	int sleep_sec = 0;
	int ret1 = 0, ret2 = 0;
	char md5_1[33] = {0};//下载包名称中包含的md5值
	char md5_2[33] = {0};//MD5sum命令获取的MD5值
	char cmd[128] = {0};
	DIR *dir;
	struct dirent *ptr;
	char filepath[128] = {0};


	sprintf(cmd,"mkdir  %s -p",DOWNLOAD_PATH);
	system(cmd);

	while(1){

		sleep_sec = SCAN_TIME;
		while(sleep_sec)
			sleep_sec = sleep(sleep_sec);

#ifdef UPDATE_DEBUG
		printf("##Tcu Update## scan update file...\n");
#endif

		dir = opendir(DOWNLOAD_PATH);
		if(dir == NULL){
			perror("##Tcu Update##open dir error...");
			sprintf(cmd,"mkdir  %s -p",DOWNLOAD_PATH);
			system(cmd);
			continue;
		}

		while((ptr = readdir(dir)) != NULL){
			if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
				continue;

			memset(md5_1,0,sizeof(md5_1));
			memset(md5_2,0,sizeof(md5_2));
			ret1 = parse_file_name_md5(md5_1,sizeof(md5_1),ptr->d_name);
			if(ret1 == 0){
				strcpy(filepath,DOWNLOAD_PATH);
				strcat(filepath,ptr->d_name);
				ret2 = get_file_md5(md5_2,sizeof(md5_2),filepath);
				printf("file name:%s\n filename md5:%s\n file md5:%s\n",filepath,md5_1,md5_2);
				if(ret2 == 0){
					puts("##Tcu Update##tcu update..."); /* prints tcu update... */
					memset(cmd,0,sizeof(cmd));
					sprintf(cmd,"tar xzvf %s -C %s",filepath,DOWNLOAD_PATH);
					puts(cmd);
					system(cmd);

					memset(cmd,0,sizeof(cmd));
					sprintf(cmd,"%sinstall.sh",DOWNLOAD_PATH);
					puts(cmd);
					system(cmd);
					system("sync");

					memset(cmd,0,sizeof(cmd));
					sprintf(cmd,"rm %sinstall.sh",DOWNLOAD_PATH);
					puts(cmd);
					system(cmd);

					memset(cmd,0,sizeof(cmd));
					sprintf(cmd,"rm %s",filepath);
					puts(cmd);
					system(cmd);
				}
			}
			else{
				memset(cmd,0,sizeof(cmd));
				sprintf(cmd,"rm %s%s",DOWNLOAD_PATH,ptr->d_name);
				system(cmd);
			}
		}

	}
	return EXIT_SUCCESS;
}
